# -*- coding: UTF-8 -*-

import copy


class EditBase(object):
    def __init__(self, viewmodel=None, record=None, values=None):
        self.viewmodel = viewmodel
        self.record = copy.deepcopy(record or {})
        self.values = copy.deepcopy(values or {})

    @property
    def Model(self):
        return self.viewmodel.Model

    def _values_display(self, record=None, values=None):
        # todo: m2m o2m 做处理

        record = copy.deepcopy(record or {})
        values = copy.deepcopy(values or {})
        record.update(values)
        return record


class EditModel(EditBase):
    def __init__(self, viewmodel=None, record=None, values=None):
        super().__init__(viewmodel=viewmodel, record=record, values=values)

    @property
    def context(self):
        return self.viewmodel.context

    def onchange_new(self):
        context = self.context

        Model = self.Model
        result = Model.web_onchange_new(context=context)
        self.record = {}
        self.values = result['values']
        return {'record': {}, 'values': self.values}

    def set_editable(self, record=None):
        self.record = copy.deepcopy(record or {})
        self.values = {}
        return self._values_display(record, {})

    def onchange(self, fname, value):
        self.values[fname] = value
        context = self.context
        Model = self.Model

        res_id = self.record.get('id')
        res_ids = res_id and [res_id] or []

        result = Model.web_onchange(
            fname, value, res_ids=res_ids,
            record=self.record, values=self.values,
            context=context)
        values = result['values']
        self.values = copy.deepcopy(values)
        return {'record': self.record, 'values': self.values}

    def commit(self, **kwargs):
        context = self.context
        res_id = self.record.get('id')

        record = self.record
        values = self.values
        values2 = self.viewmodel._get_values_for_write(record, values)

        Model = self.Model
        result = Model.web_commit(
            res_id, values2, record=record, context=context)

        return result


class EditX2m(EditBase):
    def __init__(self, viewmodel=None, record=None, values=None, parentData=None):
        super().__init__(viewmodel=viewmodel, record=record, values=values)
        self.parentData = parentData or {}

    @property
    def context(self):
        return self.viewmodel.context_get(self.parentData)

    def context_get(self, parentData):
        return self.viewmodel.context_get(parentData)

    def _values_with_parent(self, parentData):
        parent = self.viewmodel.parent
        relation_field = self.viewmodel.field_info['relation_field']
        parent_values = parent.Model.get_values_for_onchange(**parentData)

        # print('xxxxx', parent)
        # print('xxxxx1', parentData)
        # print('xxxxx', parent)
        # print('xxxxx2', relation_field)
        # print('xxxxx3', parent_values)

        return {relation_field: parent_values}

    def onchange_new(self, parentData):
        context = self.context_get(parentData)
        values = self._values_with_parent(parentData)

        Model = self.Model
        result = Model.web_onchange_new(values=values, context=context)

        values_ret = result['values']

        self.record = {}
        self.values = copy.deepcopy(values_ret)
        values2 = copy.deepcopy(values_ret)

        relation_field = self.viewmodel.field_info['relation_field']
        del values2[relation_field]
        res = {'record': {}, 'values': values2}
        # print('new:', res)
        return res

    def onchange(self, fname, value):
        self.values[fname] = value
        context = self.context
        Model = self.Model

        res_id = self.record.get('id')
        # 需要 判断 是虚拟id
        res_ids = res_id and [res_id] or []

        result = Model.web_onchange(
            fname, value, res_ids=res_ids,
            record=self.record, values=self.values,
            context=context)

        values_ret = result['values']

        self.record = {}
        self.values = copy.deepcopy(values_ret)
        values2 = copy.deepcopy(values_ret)

        relation_field = self.viewmodel.field_info['relation_field']
        del values2[relation_field]

        # print('result', result)
        return {'record': self.record, 'values': values2}

    def _x2m_tuple_get(self):
        res_id = self.record.get('id')
        values = self.values
        relation_field = self.viewmodel.field_info['relation_field']
        values2 = copy.deepcopy(values)
        del values2[relation_field]

        if res_id:
            return (1, res_id, values2)
        else:
            return (0, False, values2)

    def commit(self):
        return self._x2m_tuple_get()
